package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootSkill;
import org.footballmanager.pandafootball.system.form.impl.FootSkillSearchForm;
import org.footballmanager.pandafootball.system.service.FootSkillService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootSkillServiceImpl extends ServiceImpl<FootSkill> implements FootSkillService {
    /**
     * 根据技能名称查询记录
     * @param name 技能名称
     * @return 可用技能列表
     */
    @Override
    public List<FootSkill> findByName(String name) {
        Example example = new Example(FootSkill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        criteria.andEqualTo("use", true);
        example.orderBy("sort asc");
        return this.findall(example);
    }

    /**
     * 根据技能名称查询记录
     *
     * @param name 技能名称
     * @return 所有技能列表
     */
    @Override
    public List<FootSkill> findAllSkillByName(String name) {
        Example example = new Example(FootSkill.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        example.orderBy("sort asc");
        return this.findall(example);
    }

    /**
     * 查询
     *
     * @param entityname          实体类名
     * @param map                 参数
     * @param footSkillSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootSkillSearchForm footSkillSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footSkillSearchForm);
        Page<FootSkill> page = PageHelper.startPage(footSkillSearchForm.getPageIndex(), footSkillSearchForm.getPageSize());
        List<FootSkill> list = this.findall(footSkillSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footSkillSearchForm 条件
     * @param request             请求
     */
    @Override
    public void deleteAll(FootSkillSearchForm footSkillSearchForm, HttpServletRequest request) {
        if(footSkillSearchForm.getSelectIds() != null){
            String[] result = footSkillSearchForm.getSelectIds().split(Constants.COMMA);
            for(String id: result){
                this .delete(Long.parseLong(id), request);
            }
        }
    }
}
